<?php 
$connection = Yii::app()->db;

$oDeporte = DeporteActividad::model()->findByPk($deporte);
echo "<h1>";
?>
<img align="middle" alt="<?php echo $oDeporte->deporte_actividad_observacion; ?>"  src="<?php echo Yii::app()->theme->baseUrl.'/images/'.strtolower($oDeporte->deporte_actividad_observacion).'.png'; ?>" />
<?php 
echo "MEDALLERO DE ".strtoupper($oDeporte->deporte_actividad_descripcion)."</h1>";
?>	
<table id="medallero">
<thead>
<tr>
	<th scope="col">PUESTO</th>
	<th scope="col">CON</th>
	<th scope="col">ORO</th>
	<th scope="col">PLATA</th>
	<th scope="col">BRONCE</th>
	<th scope="col">MEDALLAS TOTALES</th>		
</tr>
</thead>
<tbody>
<?php
$totalOro=0;
$totalPlata=0;
$totalBronce=0;
$totalTotal=0;
foreach ($orden as $pais)
{
	
		$command = $connection->createCommand("
		SELECT gp.pais_id,gp.pais_nombre,sum(grm.grid_oro) as oro,sum(grm.grid_plata) as plata,sum(grm.grid_bronce) as bronce
		    FROM grid_r_medallas grm 
        
        inner join grid_r_entidad gre 
        on gre.grid_entidad_id = grm.grid_entidad_id
        
        inner join grid_pais gp
        on gp.pais_id = gre.grid_pais_id
                    
        where gre.grid_deporte_actividad_id=$deporte and gp.pais_id=".$pais->pais_id."
        
        group by pais_id,pais_nombre");

		$dataReader = $command->query();
		
		$row=$dataReader->read();
			
		?>
		<tr>
			<td align="center"><?php echo $pais->orden?></td>
			
			<td >
			<?php echo '<a href="'.Yii::app()->createUrl('medallero/medallistas',array('deporte'=>$deporte,'pais'=>$pais->pais_id)).'">';?>
		<img align="left" alt="<?php echo $row['pais_nombre']; ?>"  src="<?php echo Yii::app()->theme->baseUrl.'/images/'.substr(strtolower($row['pais_nombre']),0,3).'.png'; ?>" />	
		<?php echo $row['pais_nombre'];
		echo '</a>';?></td>							
			
			<td align="center"><b><?php if($row['oro']!='')
						echo $row['oro'];
						else echo '0'; ?></b></td>
			
			<td align="center"><b><?php if($row['plata']!='')
						echo $row['plata'];
						else echo '0'; ?></b></td>
			
			<td align="center"><b><?php if($row['bronce']!='')
						echo $row['bronce'];
						else echo '0'; ?></b></td>
				
			<td align="center"><b><?php echo $row['oro']+$row['plata']+$row['bronce']?></b></td>	
		</tr>		
<?php 	
$totalOro+=$row['oro'];
$totalPlata+=$row['plata'];
$totalBronce+=$row['bronce'];
$totalTotal	+=$row['oro']+$row['plata']+$row['bronce'];
}
?>
</tbody>
<tfoot>
		<!-- Zona Totales -->
		<tr>
			<td colspan="2" align="center">TOTALES</td>
			<td align="center"><b><?php echo $totalOro?></b></td>
			
			<td align="center"><b><?php echo $totalPlata?></b></td>
			
			<td align="center"><b><?php echo $totalBronce?></b></td>
				
			<td align="center"><b><?php echo $totalTotal?></b></td>				
		</tr>
</tfoot>
</table>